<template>
  <view class="container">
    <!-- 加载状态 -->
    <view v-if="loading" class="loading">
      <text>加载中...</text>
    </view>

    <!-- 用户信息 -->
    <view class="user-info" v-else-if="userInfo">
      <image class="avatar" :src="userInfo.img" mode="aspectFill"></image>
      <text class="username">{{ userInfo.user }}</text>
    </view>

    <!-- 未登录提示 -->
    <view v-else class="not-logged-in">
      <text>您尚未登录，请登录后再试。</text>
    </view>

    <!-- 导航菜单 -->
    <view class="menu-list">
      <view
        v-for="(item, index) in menuItems"
        :key="index"
        class="menu-item"
        @tap="goToPage(item.url)"
      >
        <text>{{ item.label }}</text>
      </view>
    </view>

    <!-- 发布按钮 -->
    <view class="publish-button" @tap="goToPage('/pages/publish/publish')">
      <text class="plus-icon">+</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      userInfo: null,
      loading: true,
      menuItems: [
        { label: '我的发布', url: '/pages/personal/minetopic' },
        { label: '账号管理', url: '/pages/accountManage/accountManage' },
        { label: '设置', url: '/pages/settings/settings' }
      ]
    };
  },
  onLoad() {
    this.fetchUserInfo();
  },
  methods: {
    fetchUserInfo() {
      this.loading = true; // 显示加载状态
      const userInfo = getApp().globalData.userInfo;
      if (userInfo) {
        this.userInfo = userInfo;
      } else {
        uni.showToast({
          title: '用户未登录或信息已过期',
          icon: 'none'
        });
        uni.relaunch({
          url: '/pages/login/login'
        });
      }
      this.loading = false; // 隐藏加载状态
    },
    goToPage(url) {
      uni.navigateTo({
        url: url
      });
    }
  }
};
</script>

<style>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20rpx;
  position: relative; /* 设置为相对定位，用于绝对定位的发布按钮 */
}

.user-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 40rpx;
}

.avatar {
  width: 150rpx;
  height: 150rpx;
  border-radius: 50%;
  margin-bottom: 20rpx;
}

.username {
  font-size: 20px;
  font-weight: bold;
}

.menu-list {
  width: 100%;
}

.menu-item {
  padding: 20rpx;
  border-bottom: 1px solid #eee;
  font-size: 18px;
  text-align: center;
}

/* 加载状态样式 */
.loading {
  margin-top: 20rpx;
  font-size: 16px;
  color: #999;
}

/* 未登录提示样式 */
.not-logged-in {
  margin-top: 20rpx;
  font-size: 16px;
  color: #ff0000;
}

/* 发布按钮样式 */
.publish-button {
  position: absolute; /* 绝对定位 */
  right: 40rpx; /* 距离右边的距离 */
  bottom: 10rpx; /* 距离底部的距离 */
  width: 100rpx;
  height: 100rpx;
  background-color: #007aff; /* 按钮背景颜色 */
  border-radius: 50%; /* 圆形按钮 */
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 0 10rpx rgba(0, 0, 0, 0.2); /* 阴影效果 */
}

.plus-icon {
  font-size: 48rpx;
  color: #ffffff;
  font-weight: bold;
}
</style>